﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.UI;
using YArchitech.Revit.Electric.Common;

namespace YArchitech.MEP.Electric.CableLay
{
	public class Application : IExternalApplication
	{
		public Result OnStartup(UIControlledApplication application)
		{
			Application.thisApp = this;
			this.m_mainForm = null;
			this.m_ELForm = null;
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public Result OnShutdown(UIControlledApplication application)
		{
			if (this.m_mainForm != null && this.m_mainForm.Visible)
			{
				this.m_mainForm.Close();
			}
			if (this.m_ELForm != null && this.m_ELForm.Visible)
			{
				this.m_ELForm.Close();
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public CableLayMainForm ThisMainForm
		{
			get
			{
				return this.m_mainForm;
			}
		}

		public void ShowMainForm(UIApplication uiApp)
		{
			if (uiApp == null)
			{
				return;
			}
			if (this.m_mainForm == null || this.m_mainForm.IsDisposed)
			{
				RequestHandler requestHandler = new RequestHandler();
				ExternalEvent exEvent = ExternalEvent.Create(requestHandler);
				this.m_mainForm = new CableLayMainForm(uiApp.ActiveUIDocument, exEvent, requestHandler);
				this.m_mainForm.Show(WindowHelper.HWndRevit);
			}
		}

		public void ReShowMainForm()
		{
			if (this.m_mainForm == null || this.m_mainForm.IsDisposed)
			{
				return;
			}
			this.m_mainForm.Show(WindowHelper.HWndRevit);
		}

		public void HideMainForm()
		{
			if (this.m_mainForm == null || this.m_mainForm.IsDisposed)
			{
				return;
			}
			this.m_mainForm.Hide();
		}

		public void ResetCableListData(CableListData data)
		{
			if (data == null)
			{
				return;
			}
			if (this.m_lstData == null)
			{
				this.m_lstData = new List<CableListData>();
			}
			else
			{
				this.m_lstData.Clear();
			}
			this.m_lstData.Add(data);
		}

		public void ResetCableListData(List<CableListData> lstData)
		{
			if (lstData == null || lstData.Count <= 0)
			{
				return;
			}
			if (this.m_lstData == null)
			{
				this.m_lstData = new List<CableListData>();
			}
			else
			{
				this.m_lstData.Clear();
			}
			foreach (CableListData item in lstData)
			{
				this.m_lstData.Add(item);
			}
		}

		public EquipmentListForm ThisELForm
		{
			get
			{
				return this.m_ELForm;
			}
		}

		public void ShowEquipmentListForm(UIApplication uiApp)
		{
			if (uiApp == null)
			{
				return;
			}
			if (this.m_ELForm == null || this.m_ELForm.IsDisposed)
			{
				RequestHandler requestHandler = new RequestHandler();
				ExternalEvent exEvent = ExternalEvent.Create(requestHandler);
				this.m_ELForm = new EquipmentListForm(uiApp.ActiveUIDocument, exEvent, requestHandler);
				this.m_ELForm.Show(WindowHelper.HWndRevit);
			}
		}

		internal static Application thisApp;

		private CableLayMainForm m_mainForm;

		private EquipmentListForm m_ELForm;

		public List<CableListData> m_lstData;
	}
}
